Datatypes in Python

What you should learn: floats, integers, long, complex, bools, strings

Int and Float - Calculate the following expression using Python:

$y = 1-(5*(6-12))+5/7$


In [ ]:

Check the result manually! If it is incorrect think about what might be the reason and correct the code. If the result is correct what is important point here we have to take care of?


In [ ]:

Modulo - Calculate the following expression using Python:

$ 8674^5\mod 67$


In [ ]:

Complex - Calculate the following expression using Python:

$((5+3j) - (7-2j)) * (4+6j)$


In [ ]:

Bool - Define two bool variables a and b

$a = True,b = False$

and calulate (!a = not a)

$ (a \vee b ) \wedge (a \wedge !a)$

and

$ (a - b ) + (a + !a)$


In [ ]:


In [ ]:

String - define the two variables

$name = Max, surname = Mustermann$

Concatenate them with a whitespace and print the result


In [ ]:

Bit manipulation - define the variables

$x = 13, y = 5$

and shift the bits of x by 2 to the right and the bits y by one to the left

calculate logic 'and', 'or' and 'xor' of x and y


In [ ]:

Complex datastructures in Python

What you should learn: List, dictionaries, maps, tuples, indexing

Lists - Define the two lists

$A = [1,2,3,4,5], B = [10,9,8,7,6]$

concaltenate them, store the result in C and print C


In [ ]:

Extract the sublist [3,4,5,10] to List D

Add the element at position 8 (start counting from 0) of list C at the end of D


In [ ]:

Add element 5 at the end of the list

Remove element 5

Insert 15 at position 1 (start counting from 0)

Sort the list


In [ ]:

Remove the element at position 2 and reverse the list


In [ ]:

Sets

Create the two sets A = 'Apple','Peach','Banana','Blueberry' and B = 'Strawberry','Peach','Banana','Pineapple' and check wheather A and B contain the lement 'Pineapple'


In [ ]:

Now calculate

$A \, \bigcup \, B$ (Union), $A \, \bigcap \, B$ (Intersection), $A \, \setminus \, B$ (Difference), $A \, \triangle \, B = (A \, \setminus \, B) \, \bigcup \, (B \, \setminus \, A)$ (Symmetric difference)


In [ ]:

Dictioneries - Create a dictionary

Create a dictionary with the key-value pairs

$pen-567, paper-673, keyboard-52$,

Add the key-value pair monitor-4,

Delete the key pen, prin the dictionary keys

Check wheather paper is an element of dict and print its value.


In [ ]:

Control flows and functions

Create a list A with even and a list B with odd numbers between 0 and 20 (both exklusive)


In [ ]:


In [ ]:

While this task can be solved easily with loops and if-else statements, many Python coders insist that a more pythonic way would avoid such loops. See the notebook on list comprehension on one common way of avoiding loops.

Implement the same using a while loop


In [ ]:

Now loop through B, print the number until unit you find the number 17 using a for loop break and continue


In [ ]:

Function - Define the following polynome as a Python function with default value for x = 3:

$f(x) = 5*x^2 - 4*\mid x^3\mid + \frac{1}{10}x^5$

a function that calculates f(x) and prints it:

$y = f(5)$


In [ ]:

Reimplement the function such that it returns a lambda expression


In [ ]:

Note that python allows to use various functions like cos, sin, exp ... however this will be covered when we dive into numpy later on

Plotting - Plot the polynome in the range -10 to 20 using list comprehensions


In [ ]:

Iterators - Use the zip function to loop over two variables list simultaniously

$A = 1,2,3,4,5, B = A,B,C,D,E$


In [ ]:

Use sorted, reversed and enumerate to loop over B in reverse sorted order while printing the current index


In [ ]:

use map to get all values of the polynome f in the range 1..10


In [ ]:

Loading / Saving Images


In [ ]:

Modules in Python

Do a wild import of scipy

Import the method scipy.misc.imread() and name it image_read the module scipy and load an image from hard disk

Create you own module containing the function image_read and import the function from this module.


In [ ]:

OOP

Create a 'abstract' class Object3D that implemets the class method get_volume(). Notice that python does not allow abstract classes so so can mimic that by throwing a not implemented error for the method get_volume(). Implement a class method get_base_class() that returns the name of the class as string.

Now create a class Box, which inherits from Object3D. The constructor takes the 3 different edge length and stores them in object variables and get_volume will return its volume.

Now create a class Cube which inherits from Box. The constructore take the length of the edges and wil call the super constructor with this value.

Create a Object3D, a Box with size 3,6,7 and a Cube with size 6, call get_volume() and the get_base_class() methods.


In [ ]:


In [ ]: